Sunday, December 9, 2018

ElementRef became a generic in Angular 6!

If you see this error in Visual Studio Code...

[ts] Generic type 'ElementRef<T, any>' requires 2 type arguement (s).

 
 

...and it seems like this won't fly...

@ViewChild('foo') foo: ElementRef;

 
 

...try this instead:

@ViewChild('foo') foo: ElementRef<HTMLElement, any>;

 
 

You will then be able to use it as you might expect.

this.foo.nativeElement.style.display = "none";

 
 

I write again. On second thought...

@ViewChild('foo') foo: ElementRef<HTMLElement>;

 
 

...is probably better. Visual Studio Code still acts like there is a problem, but the compiler compiles for TypeScript 2.7.2. while I cannot do so with the any jammed in there. Wait, I closed and reopened Visual Studio Code after...

npm install typescript@2.7.2

 
 

...and now Visual Studio Code is happy too. Wait. I spoke too soon. More later maybe.

 
 

Addendum 12/10/2018: To fix the Visual Studio Code problem just uninstall Visual Studio Code and then go get it from the web anew.

No comments:

Post a Comment